package com.keyancompany.zhongbaopingtai.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;

import com.keyancompany.zhongbaopingtai.AppManager;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.utils.IntentUtils;
import com.keyancompany.zhongbaopingtai.utils.SPUtils;
import com.photoselector.util.CommonUtils;

public class WelcomeActivity extends Activity {

    boolean isFirstIn = false;

    private static final int GO_LOGIN = 1000;
    private static final int GO_GUIDE = 1001;
    private static final int GO_HOME = 1002;

    // 延迟3秒
    private static final long SPLASH_DELAY_MILLIS = 2000;

    /**
     * Handler:跳转到不同界面
     */
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_LOGIN:
                    goLogin();
                    break;
                case GO_GUIDE:
                    goGuide();
                    break;
                case GO_HOME:
                    goHome();
                    break;
            }
        }
    };

    private void goLogin() {
        IntentUtils.startActivity(this, LoginActivity.class,
                Constants.IS_FROM_LOGIN, true);
        finish();
    }

    private void goHome() {
        IntentUtils.startActivity(this, MainActivity.class,
                Constants.IS_FROM_LOGIN, false);
        finish();
    }

    private void goGuide() {
        Intent intent = new Intent(this, GuideActivity.class);
        startActivity(intent);
        finish();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 防止第三方跳转时出现双实例
        Activity aty = AppManager.getActivity(WelcomeActivity.class);
        if (aty != null && !aty.isFinishing()) {
            finish();
        }
        // SystemTool.gc(this); //针对性能好的手机使用，加快应用相应速度

        final View view = View.inflate(this, R.layout.activity_welcome, null);
        setContentView(view);
        // 渐变展示启动屏
        AlphaAnimation aa = new AlphaAnimation(0.5f, 1.0f);
        aa.setDuration(800);
        view.startAnimation(aa);
        aa.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationEnd(Animation arg0) {
                boolean isFirstInMain = (boolean) SPUtils.getCommData(WelcomeActivity.this, Constants.IS_FROM_MAIN, false);

                // 判断程序与第几次运行，如果是第一次运行则跳转到引导界面，否则跳转到主界面
                String value = (String) SPUtils.getCommData(WelcomeActivity.this,
                        Constants.USER, "");
                Log.e("hpt" , "____________________ "+value +" ///// "+isFirstInMain);

                // 取得相应的值，如果没有该值，说明还未写入，用true作为默认值
                isFirstIn = (boolean) SPUtils.getCommData(WelcomeActivity.this,"isFirstIn", true);
                // 判断程序与第几次运行，如果是第一次运行则跳转到引导界面，否则跳转到主界面
                if (!isFirstIn) {
                    if (TextUtils.isEmpty(value)) {
                        goLogin();
                        return;
                    }
                    if (!isFirstInMain) {
                        // 使用Handler的postDelayed方法，3秒后执行跳转到MainActivity
                        /** 跳转到 登录界面 */
                        mHandler.sendEmptyMessageDelayed(GO_LOGIN, SPLASH_DELAY_MILLIS);
                    } else {
                        /*** 这里跳转到 主页 */
                        mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
                    }
                } else {
                    /** 这里跳转到向导界面 */
                    mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);
                }

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationStart(Animation animation) {
            }
        });
    }
}
